/**
 * 
 */
package co.edu.udistrital.analyzer.business.services.impl;

import java.io.Serializable;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import co.edu.udistrital.analyzer.business.services.AuthenticationService;

/**
 * @author Alejo
 * 
 */
@Service("authenticationService")
public class AuthenticationServiceImpl implements AuthenticationService, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -131858013605099816L;

	/**
	 * The log4j Logger
	 */
	private static final Logger LOGGER = Logger.getLogger(AuthenticationService.class);

	@Resource(name = "authenticationManager")
	private AuthenticationManager authenticationManager; // specific for Spring
															// Security

	@Override
	public boolean login(String username, String password) {
		try {
			Authentication authenticate = authenticationManager
					.authenticate(new UsernamePasswordAuthenticationToken(
							username, password));
			if (authenticate.isAuthenticated()) {
				SecurityContextHolder.getContext().setAuthentication(
						authenticate);
				return true;
			}
		} catch (AuthenticationException e) {
			LOGGER.error("Error authenticating", e);
		}
		return false;
	}
}
